package it.unical.mat.aec.manageResult;

import it.unical.mat.aec.model.Test;

import it.unical.mat.aec.persistence.FileManagerException;
import it.unical.mat.aec.persistence.TestFileManagerSingleton;


import java.io.FileNotFoundException;
import java.util.ArrayList;

public class ManageResultFacade {

	public static final String ORDINAMENTO_PER_VOTO="OrdinaVoto";
	public static final String ORDINAMENTO_PER_STUDENTE="OrdinaStudente";
	
	private static ManageResultFacade manageResultFacade;
	
	private String pathFile;

	private ArrayList<Test> tests;
	private EsportaTests esportaTests;

	private ManageResultFacade() {
		tests = new ArrayList<Test>();
	}

	public static ManageResultFacade getInstance() {
		if (manageResultFacade == null)
			manageResultFacade = new ManageResultFacade();
		return manageResultFacade;
	}

	public boolean caricaTests(String path)  {
		try {
			tests = TestFileManagerSingleton.getInstance().caricaTests(path);
			pathFile=path;
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public Test getTest(int idTest) {
		for(Test t:tests){
			if(t.getId()==idTest)
				return t;
		}
		return null;
	}

	public Test getTestMatricola(int matricola) {
		for(Test t:tests){
			if(t.getStudente().getMatricola()==matricola)
				return t;
		}
		return null;
	}
	
	//aggiunto da aggiungere nell'analisi

	public ArrayList<Test> getTestStudente(String string) {
		ArrayList<Test> tests=new ArrayList<Test>();
		for(Test t:this.tests){
			if(t.getStudente().getCognome().equalsIgnoreCase(string) || t.getStudente().getNome().equalsIgnoreCase(string))
				tests.add(t);
		}
		return tests;
	}
	
	public boolean salvaTests(String path){
		try {
			TestFileManagerSingleton.getInstance().salvaTests(path, tests);
			return true;
		} catch (Exception e) {
			return false;
		}

	}


	public void cancellaTests(String path) {
		tests.removeAll(tests);
		TestFileManagerSingleton.getInstance().cancellaTests(path);
	}

	public ArrayList<Test> cancellaTest(int idTest, String path) throws FileManagerException {
		tests.remove(idTest);
		TestFileManagerSingleton.getInstance().cancellaTests(path);
		TestFileManagerSingleton.getInstance().salvaTests(path, tests);
		return tests;
	}

	public void esportaTests(String path) throws FileNotFoundException {
		esportaTests.esportaTests(tests, path);
	}

	
	public ArrayList<Test> ordinaTests(String tipoOrdine,boolean crescente) throws OrdinaException {
		String nameClass="it.unical.mat.aec.manageResult."+tipoOrdine;
		try {
			OrdinaTemplateMethod ordina=(OrdinaTemplateMethod) Class.forName(nameClass).newInstance();
			return ordina.ordinaTests(tests, crescente);
		} catch (Exception e) {
			throw new OrdinaException("Ordered fail ,type not found");
		}

	}

	public void setEsportaTest(EsportaTests esporta) {
		esportaTests = esporta;
	}

	public ArrayList<Test> getTests() {
		return tests;
	}

	public String getPathFile() {
		return pathFile;
	}

	public void setTests(ArrayList<Test> tests) {
		this.tests = tests;
	}


}
